package com.llkj.qingnianguoxue.circle;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.InviteFriendsAdapter;
import com.llkj.qingnianguoxue.adapter.MyFriendsAdapter;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.bean.InviteFriendBean;
import com.llkj.qingnianguoxue.bean.MyFriendListBean;
import com.llkj.qingnianguoxue.bean.response.ResponseList;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.StringUtils;
import com.llkj.qingnianguoxue.util.ToastUtil;
import com.llkj.qingnianguoxue.util.helper.LoadDataHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * 邀请好友
 */
public class InviteFriendsActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
    private SwipeRefreshLayout refreshLayout;
    private RecyclerView lvMessage;
    private InviteFriendsAdapter friendsAdapter;
    private List<InviteFriendBean> friendData;
    private String mCircleId;//圈子id

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_friends);
        setTitle("邀请好友", true, TITLE_TYPE_IMG, R.drawable.icon_back, false, TITLE_TYPE_TEXT, null);
        Intent intent = getIntent();
        mCircleId = intent.getStringExtra("circleId");

        initView();
        initListener();

        loadWaitDialog();
        onRefresh();
    }

    private void initView() {
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
        refreshLayout.setColorSchemeResources(R.color.blue3989ff);
        lvMessage = (RecyclerView) findViewById(R.id.recyclerView);
        lvMessage.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        friendData = new ArrayList<>();
        friendsAdapter = new InviteFriendsAdapter(R.layout.item_invite_friend, friendData);
        lvMessage.setAdapter(friendsAdapter);
    }

    private void initListener() {
        refreshLayout.setOnRefreshListener(this);
        lvMessage.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                String friendId = friendData.get(i).getId();
                inviteFriend(friendId);
            }
        });
    }

    @Override
    protected void dialogRightBtn() {
        super.dialogRightBtn();

    }

    @Override
    public void onRefresh() {
        friendData.clear();
        getFriendsData();
    }

    /**
     * 获取好友数据
     */
    private void getFriendsData() {
        HttpMethod.inviteFriend(this, this, mCircleId);
    }

    private void inviteFriend(String friendId){
        loadWaitDialog();
        HttpMethod.inviteFriendReq(this,this,mCircleId,friendId);
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        refreshLayout.setRefreshing(false);
        if (method == HttpMethod.HTTP_CIRCLE_IS_INVITING) {//邀请好友
            ResponseList responseList = GsonUtil.fromJsonList(data,InviteFriendBean.class);
            if(responseList!=null){
                friendData.addAll(responseList.getInviting_list());
                friendsAdapter.notifyDataSetChanged();
            }
        }else if(method == HttpMethod.HTTP_CIRCLE_INVITING){//邀请好友加入圈子
            ToastUtil.makeLongText(this,"邀请已发送");
        }
    }

    @Override
    protected void onhttpFailed(String message, int method) {
        super.onhttpFailed(message, method);
        if (!StringUtils.isEmpty(message)) {
            ToastUtil.makeLongText(this, message);
        }
    }

}
